package com.yanqu.road.server.cmd.activity.unionwar;

import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.unionwar.UnionWarActivityServerProto;
import com.yanqu.road.pb.rank.UnionActivityRankProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;
import java.util.Objects;

@Cmd(code = GameProtocol.S_UNION_WAR_ACTIVITY_GET_RANK_LIST, desc = "群英争霸获取排行榜列表")
public class UnionWarGetRankListCmd extends AbstractCommand {
    private int activityId;
    private int stage;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        UnionWarActivityServerProto.GetRankListReqMsg msg = UnionWarActivityServerProto.GetRankListReqMsg.parseFrom(packet.getBytes());
        activityId = msg.getActivityId();
        stage = msg.getStage();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        UnionWarActivityServerProto.GetRankListReqMsg.Builder crossReqMsg = UnionWarActivityServerProto.GetRankListReqMsg.newBuilder();
        crossReqMsg.setActivityId(activityId);
        crossReqMsg.setStage(stage);
        String unionUid = GameUnionActivityGroupMgr.getUnionUid(activityId, packet.getPlayerId());
        crossReqMsg.setUnionUid(unionUid);
        int groupId = GameUnionActivityGroupMgr.getViewDataGroupId(activityId, packet.getPlayerId());
        player.sendUnionActivityGroupPacket(Protocol.C_UNION_WAR_ACTIVITY_GET_RANK_LIST, crossReqMsg, activityId, groupId);
//        CrossActivityProto.UnionWarRankListRespMsg.Builder respMsg = CrossActivityProto.UnionWarRankListRespMsg.newBuilder();
//        CrossActivityProto.UnionWarRankMsgMsg.Builder unionWarRankListMsg = CrossDataMgr.getUnionWarRankListMsg(activityId, stage);
//        ActivityInfo activityInfo = UnionWarActivityMgr.getOpenActivityInfo(activityId);
//        int maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
//        boolean lockUnion = false;
//        if (activityInfo != null) {
//            int conditionType = UnionWarActivityMgr.getConditionTypeByPhase(stage);
//            maxRank = UnionWarActivityMgr.getMaxRank(activityId, conditionType);
//            CurrentTimeInfo currentTimeInfo = UnionPhaseWarMgr.getUnionWar(activityId).getCurrentTimeInfo();
//            if (currentTimeInfo.getTimeInfo() != null) {
//                if (currentTimeInfo.getTimeInfo().getType() > stage) {
//                    lockUnion = true;
//                }
//            } else if (DateHelper.getCurrentSecond() >= activityInfo.getEndTime()) {
//                lockUnion = true;
//            }
//        }
//        UnionActivityRankProto.UnionActivityRankListMsg.Builder rankListMsg = unionWarRankListMsg.getRankList().toBuilder();
//        String value = getMyCrossUnionValue(player, rankListMsg);
//        respMsg.setValue(value);
//        while (null != rankListMsg.getRankListList() && rankListMsg.getRankListCount() > maxRank) {
//            rankListMsg.removeRankList(rankListMsg.getRankListCount() - 1);
//        }
//        respMsg.setRankList(rankListMsg);
//        int rank = -1;
//        String unionUid = player.getUserInfo().getUnionUid();
//        if (lockUnion) {
//            unionUid = UnionPhaseWarMgr.getUnionWar(activityId).isCanGetRewardMember(unionUid, stage, player.getUserId());
//        }
//        if (!StringUtils.isNullOrEmpty(unionUid)) {
//            rank = ActivityMgr.getMyCrossUnionRank(unionUid, rankListMsg);
//        }
//        respMsg.setMyRank(rank);
//        respMsg.setActivityId(activityId);
//        respMsg.setStage(stage);
//        player.sendChannelPacket(ClientProtocol.U_UNION_WAR_ACTIVITY_GET_RANK_LIST, respMsg);
    }

    public String getMyCrossUnionValue(GamePlayer player, UnionActivityRankProto.UnionActivityRankListMsg.Builder rankListMsg) {
        String unionUid = player.getUserInfo().getUnionUid();
        List<UnionActivityRankProto.UnionActivityRankTempMsg> rankList = rankListMsg.getRankListList();
        if (!StringUtils.isNullOrEmpty(unionUid)) {
            for (UnionActivityRankProto.UnionActivityRankTempMsg rankDataMsg : rankList) {
                if (Objects.equals(rankDataMsg.getUnionUid(), unionUid)) {
                    return rankDataMsg.getValue();
                }
            }
        }
        return "0";
    }

    @Override
    public void sendData(GamePlayer player) {


    }
}
